set(PROJ_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(SRCS_C "")
set(SRCS_CPP "")
set(SRCS_COMPILE_OPTIONS "")
set(INCLUDE_DIRS ${PROJ_SRC_DIR})

#
# AI Framework
#
if(CONFIG_ESP_BROOKESIA_ENABLE_AI_FRAMEWORK)
    set(AI_FRAMEWORK_SRC_DIR ${PROJ_SRC_DIR}/ai_framework)
    list(APPEND INCLUDE_DIRS ${AI_FRAMEWORK_SRC_DIR})
    # Agent
    if(CONFIG_ESP_BROOKESIA_AI_FRAMEWORK_ENABLE_AGENT)
        set(AI_FRAMEWORK_AGENT_SRC_DIR ${AI_FRAMEWORK_SRC_DIR}/agent)
        file(GLOB_RECURSE AI_FRAMEWORK_AGENT_SRCS_C ${AI_FRAMEWORK_AGENT_SRC_DIR}/*.c)
        file(GLOB_RECURSE AI_FRAMEWORK_AGENT_SRCS_CPP ${AI_FRAMEWORK_AGENT_SRC_DIR}/*.cpp)
        list(APPEND SRCS_C ${AI_FRAMEWORK_AGENT_SRCS_C})
        list(APPEND SRCS_CPP ${AI_FRAMEWORK_AGENT_SRCS_CPP})
    endif()
    # Expression
    if(CONFIG_ESP_BROOKESIA_AI_FRAMEWORK_ENABLE_EXPRESSION)
        set(AI_FRAMEWORK_EXPRESSION_SRC_DIR ${AI_FRAMEWORK_SRC_DIR}/expression)
        file(GLOB_RECURSE AI_FRAMEWORK_EXPRESSION_SRCS_C ${AI_FRAMEWORK_EXPRESSION_SRC_DIR}/*.c)
        file(GLOB_RECURSE AI_FRAMEWORK_EXPRESSION_SRCS_CPP ${AI_FRAMEWORK_EXPRESSION_SRC_DIR}/*.cpp)
        list(APPEND SRCS_C ${AI_FRAMEWORK_EXPRESSION_SRCS_C})
        list(APPEND SRCS_CPP ${AI_FRAMEWORK_EXPRESSION_SRCS_CPP})
    endif()
endif()

#
# GUI
#
if(CONFIG_ESP_BROOKESIA_ENABLE_GUI)
    set(GUI_SRC_DIR ${PROJ_SRC_DIR}/gui)
    file(GLOB GUI_SRCS_CPP ${GUI_SRC_DIR}/*.cpp)
    list(APPEND SRCS_CPP ${GUI_SRCS_CPP})
    list(APPEND INCLUDE_DIRS ${GUI_SRC_DIR})
    # Animation Player
    if(CONFIG_ESP_BROOKESIA_GUI_ENABLE_ANIM_PLAYER)
        set(GUI_ANIM_PLAYER_SRC_DIR ${GUI_SRC_DIR}/anim_player)
        file(GLOB_RECURSE GUI_ANIM_PLAYER_SRCS_C ${GUI_ANIM_PLAYER_SRC_DIR}/*.c)
        file(GLOB_RECURSE GUI_ANIM_PLAYER_SRCS_CPP ${GUI_ANIM_PLAYER_SRC_DIR}/*.cpp)
        list(APPEND SRCS_C ${GUI_ANIM_PLAYER_SRCS_C})
        list(APPEND SRCS_CPP ${GUI_ANIM_PLAYER_SRCS_CPP})
    endif()
    # Squareline
    if(CONFIG_ESP_BROOKESIA_GUI_ENABLE_SQUARELINE)
        set(GUI_SQUARELINE_SRC_DIR ${GUI_SRC_DIR}/squareline)
        # UI Components
        if(CONFIG_ESP_BROOKESIA_SQUARELINE_ENABLE_UI_COMP)
            set(GUI_SQUARELINE_UI_COMP_SRC_DIR ${GUI_SQUARELINE_SRC_DIR}/ui_comp)
            file(GLOB_RECURSE GUI_SQUARELINE_UI_COMP_SRCS_C ${GUI_SQUARELINE_UI_COMP_SRC_DIR}/*.c)
            list(APPEND SRCS_C ${GUI_SQUARELINE_UI_COMP_SRCS_C})
        endif()
        # UI Helpers
        if(CONFIG_ESP_BROOKESIA_SQUARELINE_ENABLE_UI_HELPERS)
            set(GUI_SQUARELINE_UI_HELPERS_SRC_DIR ${GUI_SQUARELINE_SRC_DIR}/ui_helpers)
            file(GLOB_RECURSE GUI_SQUARELINE_UI_HELPERS_SRCS_C ${GUI_SQUARELINE_UI_HELPERS_SRC_DIR}/*.c)
            list(APPEND SRCS_C ${GUI_SQUARELINE_UI_HELPERS_SRCS_C})
        endif()
    endif()
    # LVGL
    set(GUI_LVGL_SRC_DIR ${GUI_SRC_DIR}/lvgl)
    file(GLOB_RECURSE GUI_LVGL_SRCS_C ${GUI_LVGL_SRC_DIR}/*.c)
    file(GLOB_RECURSE GUI_LVGL_SRCS_CPP ${GUI_LVGL_SRC_DIR}/*.cpp)
    list(APPEND SRCS_C ${GUI_LVGL_SRCS_C})
    list(APPEND SRCS_CPP ${GUI_LVGL_SRCS_CPP})
    list(APPEND SRCS_COMPILE_OPTIONS "-DLV_LVGL_H_INCLUDE_SIMPLE")
    # Style
    set(GUI_STYLE_SRC_DIR ${GUI_SRC_DIR}/style)
    file(GLOB_RECURSE GUI_STYLE_SRCS_C ${GUI_STYLE_SRC_DIR}/*.c)
    file(GLOB_RECURSE GUI_STYLE_SRCS_CPP ${GUI_STYLE_SRC_DIR}/*.cpp)
    list(APPEND SRCS_C ${GUI_STYLE_SRCS_C})
    list(APPEND SRCS_CPP ${GUI_STYLE_SRCS_CPP})
endif()

#
# Services
#
if(CONFIG_ESP_BROOKESIA_ENABLE_SERVICES)
    set(SERVICES_SRC_DIR ${PROJ_SRC_DIR}/services)
    list(APPEND INCLUDE_DIRS ${SERVICES_SRC_DIR})
    # Storage NVS
    if(CONFIG_ESP_BROOKESIA_SERVICES_ENABLE_STORAGE_NVS)
        set(SERVICES_STORAGE_NVS_SRC_DIR ${SERVICES_SRC_DIR}/storage_nvs)
        file(GLOB_RECURSE SERVICES_STORAGE_NVS_SRCS_C ${SERVICES_STORAGE_NVS_SRC_DIR}/*.c)
        file(GLOB_RECURSE SERVICES_STORAGE_NVS_SRCS_CPP ${SERVICES_STORAGE_NVS_SRC_DIR}/*.cpp)
        list(APPEND SRCS_C ${SERVICES_STORAGE_NVS_SRCS_C})
        list(APPEND SRCS_CPP ${SERVICES_STORAGE_NVS_SRCS_CPP})
    endif()
endif()

#
# Systems
#
if(CONFIG_ESP_BROOKESIA_ENABLE_SYSTEMS)
    set(SYSTEM_SRC_DIR ${PROJ_SRC_DIR}/systems)
    list(APPEND INCLUDE_DIRS ${SYSTEM_SRC_DIR})
    # Base
    set(SYSTEM_BASE_SRC_DIR ${SYSTEM_SRC_DIR}/base)
    file(GLOB_RECURSE SYSTEM_BASE_SRCS_C ${SYSTEM_BASE_SRC_DIR}/*.c)
    file(GLOB_RECURSE SYSTEM_BASE_SRCS_CPP ${SYSTEM_BASE_SRC_DIR}/*.cpp)
    list(APPEND SRCS_C ${SYSTEM_BASE_SRCS_C})
    list(APPEND SRCS_CPP ${SYSTEM_BASE_SRCS_CPP})
    # Phone
    if(CONFIG_ESP_BROOKESIA_SYSTEMS_ENABLE_PHONE)
        set(SYSTEM_PHONE_SRC_DIR ${SYSTEM_SRC_DIR}/phone)
        file(GLOB_RECURSE SYSTEM_PHONE_SRCS_C ${SYSTEM_PHONE_SRC_DIR}/*.c)
        file(GLOB_RECURSE SYSTEM_PHONE_SRCS_CPP ${SYSTEM_PHONE_SRC_DIR}/*.cpp)
        list(APPEND SRCS_C ${SYSTEM_PHONE_SRCS_C})
        list(APPEND SRCS_CPP ${SYSTEM_PHONE_SRCS_CPP})
    endif()
    if(CONFIG_ESP_BROOKESIA_SYSTEMS_ENABLE_SPEAKER)
        list(APPEND SRCS_COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:-fconcepts>")
        list(APPEND SRCS_COMPILE_OPTIONS "-Wno-write-strings")
        set(SYSTEM_SPEAKER_SRC_DIR ${SYSTEM_SRC_DIR}/speaker)
        file(GLOB_RECURSE SYSTEM_SPEAKER_SRCS_C ${SYSTEM_SPEAKER_SRC_DIR}/*.c)
        file(GLOB_RECURSE SYSTEM_SPEAKER_SRCS_CPP ${SYSTEM_SPEAKER_SRC_DIR}/*.cpp)
        list(APPEND SRCS_C ${SYSTEM_SPEAKER_SRCS_C})
        list(APPEND SRCS_CPP ${SYSTEM_SPEAKER_SRCS_CPP})
        list(APPEND INCLUDE_DIRS ${SYSTEM_SPEAKER_SRC_DIR})
    endif()
endif()

#
# Register Component
#
idf_component_register(
    SRCS ${SRCS_C} ${SRCS_CPP}
    INCLUDE_DIRS ${INCLUDE_DIRS}
    REQUIRES json esp_netif esp_wifi nvs_flash
)
include(package_manager)
cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR})

#
# Generate speaker animation assets
#
if(CONFIG_ESP_BROOKESIA_SYSTEMS_ENABLE_SPEAKER)
    set(SPEAKER_ASSETS_ANIMATIONS_DIR "${SYSTEM_SPEAKER_SRC_DIR}/assets/animations")
    spiffs_create_partition_assets(
        anim_boot
        "${SPEAKER_ASSETS_ANIMATIONS_DIR}/boot"
        FLASH_IN_PROJECT
        MMAP_FILE_SUPPORT_FORMAT ".aaf"
        IMPORT_INC_PATH "${SPEAKER_ASSETS_ANIMATIONS_DIR}"
    )
    spiffs_create_partition_assets(
        anim_emotion
        "${SPEAKER_ASSETS_ANIMATIONS_DIR}/emotion"
        FLASH_IN_PROJECT
        MMAP_FILE_SUPPORT_FORMAT ".aaf"
        IMPORT_INC_PATH "${SPEAKER_ASSETS_ANIMATIONS_DIR}"
    )
    spiffs_create_partition_assets(
        anim_icon
        "${SPEAKER_ASSETS_ANIMATIONS_DIR}/icon"
        FLASH_IN_PROJECT
        MMAP_FILE_SUPPORT_FORMAT ".aaf"
        IMPORT_INC_PATH "${SPEAKER_ASSETS_ANIMATIONS_DIR}"
    )
endif()

#
# Compile Options
#
target_compile_options(${COMPONENT_LIB}
    PUBLIC
        ${SRCS_COMPILE_OPTIONS}
)
set(SRCS_C_COMPILE_FLAGS "-Wno-format")
set(SRCS_CPP_COMPILE_FLAGS "-Wno-missing-field-initializers -Wno-format")
set_source_files_properties(
    ${SRCS_CPP}
    PROPERTIES
        COMPILE_FLAGS "${SRCS_CPP_COMPILE_FLAGS}"
)
set_source_files_properties(
    ${SRCS_C}
    PROPERTIES
        COMPILE_FLAGS "${SRCS_C_COMPILE_FLAGS}"
)
set_source_files_properties(
    ${SYSTEM_PHONE_APP_EXAMPLES_SRCS_C}
    PROPERTIES
        COMPILE_FLAGS "${SRCS_C_COMPILE_FLAGS} -Wno-unused-variable"
)
